package org.someone.office;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTArea3DChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTAreaChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTAreaSer;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTBar3DChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTBarChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTBarSer;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTBubbleChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTBubbleSer;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTDoughnutChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTLine3DChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTLineChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTLineSer;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTLvl;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTMultiLvlStrData;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTMultiLvlStrRef;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumData;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumRef;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumVal;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTOfPieChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPie3DChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPieChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPieSer;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTRadarChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTRadarSer;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTScatterChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTScatterSer;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrData;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrRef;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrVal;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTSurface3DChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTSurfaceChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTSurfaceSer;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTUnsignedInt;

public class CTChartUtil {
	public static void reloadCTChart(CTChart ctchart, XSSFSheet sheet){
		if(ctchart==null){
			return;
		}
		if(ctchart.getPlotArea() == null){
			return;
		}
		for(CTAreaChart chart:ctchart.getPlotArea().getAreaChartList()){
			for(CTAreaSer ser : chart.getSerList()){
				reloadCTAxDataSource(ser.getCat(), sheet);
				reloadCTNumDataSource(ser.getVal(), sheet);
			}
		}
		for(CTArea3DChart chart:ctchart.getPlotArea().getArea3DChartList()){
			for(CTAreaSer ser : chart.getSerList()){
				reloadCTAxDataSource(ser.getCat(), sheet);
				reloadCTNumDataSource(ser.getVal(), sheet);
			}
		}
		for(CTBarChart chart:ctchart.getPlotArea().getBarChartList()){
			for(CTBarSer ser : chart.getSerList()){
				reloadCTAxDataSource(ser.getCat(), sheet);
				reloadCTNumDataSource(ser.getVal(), sheet);
			}
		}
		for(CTBar3DChart chart:ctchart.getPlotArea().getBar3DChartList()){
			for(CTBarSer ser : chart.getSerList()){
				reloadCTAxDataSource(ser.getCat(), sheet);
				reloadCTNumDataSource(ser.getVal(), sheet);
			}
		}
		for(CTBubbleChart chart:ctchart.getPlotArea().getBubbleChartList()){
			for(CTBubbleSer ser : chart.getSerList()){
				reloadCTAxDataSource(ser.getXVal(), sheet);
				reloadCTNumDataSource(ser.getYVal(), sheet);
			}
		}
		for(CTDoughnutChart chart:ctchart.getPlotArea().getDoughnutChartList()){
			for(CTPieSer ser : chart.getSerList()){
				reloadCTAxDataSource(ser.getCat(), sheet);
				reloadCTNumDataSource(ser.getVal(), sheet);
			}
		}
		for(CTLineChart chart:ctchart.getPlotArea().getLineChartList()){
			for(CTLineSer ser : chart.getSerList()){
				reloadCTAxDataSource(ser.getCat(), sheet);
				reloadCTNumDataSource(ser.getVal(), sheet);
			}
		}
		for(CTLine3DChart chart:ctchart.getPlotArea().getLine3DChartList()){
			for(CTLineSer ser : chart.getSerList()){
				reloadCTAxDataSource(ser.getCat(), sheet);
				reloadCTNumDataSource(ser.getVal(), sheet);
			}
		}
		for(CTOfPieChart chart:ctchart.getPlotArea().getOfPieChartList()){
			for(CTPieSer ser : chart.getSerList()){
				reloadCTAxDataSource(ser.getCat(), sheet);
				reloadCTNumDataSource(ser.getVal(), sheet);
			}
		}
		for(CTPieChart chart:ctchart.getPlotArea().getPieChartList()){
			for(CTPieSer ser : chart.getSerList()){
				reloadCTAxDataSource(ser.getCat(), sheet);
				reloadCTNumDataSource(ser.getVal(), sheet);
			}
		}
		for(CTPie3DChart chart:ctchart.getPlotArea().getPie3DChartList()){
			for(CTPieSer ser : chart.getSerList()){
				reloadCTAxDataSource(ser.getCat(), sheet);
				reloadCTNumDataSource(ser.getVal(), sheet);
			}
		}
		for(CTRadarChart chart:ctchart.getPlotArea().getRadarChartList()){
			for(CTRadarSer ser : chart.getSerList()){
				reloadCTAxDataSource(ser.getCat(), sheet);
				reloadCTNumDataSource(ser.getVal(), sheet);
			}
		}
		for(CTScatterChart chart:ctchart.getPlotArea().getScatterChartList()){
			for(CTScatterSer ser : chart.getSerList()){
				reloadCTAxDataSource(ser.getXVal(), sheet);
				reloadCTNumDataSource(ser.getYVal(), sheet);
			}
		}
		for(CTSurfaceChart chart:ctchart.getPlotArea().getSurfaceChartList()){
			for(CTSurfaceSer ser : chart.getSerList()){
				reloadCTAxDataSource(ser.getCat(), sheet);
				reloadCTNumDataSource(ser.getVal(), sheet);
			}
		}
		for(CTSurface3DChart chart:ctchart.getPlotArea().getSurface3DChartList()){
			for(CTSurfaceSer ser : chart.getSerList()){
				reloadCTAxDataSource(ser.getCat(), sheet);
				reloadCTNumDataSource(ser.getVal(), sheet);
			}
		}
	}
	
	public static void reloadCTAxDataSource(CTAxDataSource ax, XSSFSheet sheet){
		if(ax.getStrRef()!=null){
			reloadCTStrRef(ax.getStrRef(), sheet);
		}
		if(ax.getMultiLvlStrRef()!=null){
			reloadCTMultiLvlStrRef(ax.getMultiLvlStrRef(), sheet);
		}
		if(ax.getNumRef() != null){
			reloadCTNumRef(ax.getNumRef(), sheet);
		}
	}
	
	public static void reloadCTNumDataSource(CTNumDataSource num, XSSFSheet sheet){
		if(num.getNumRef() != null){
			reloadCTNumRef(num.getNumRef(), sheet);
		}
	}
	
	public static void reloadCTMultiLvlStrRef(CTMultiLvlStrRef ref, XSSFSheet sheet){
		if(ref == null){
			return;
		}
		String range = ref.getF();
		CellRangeAddress cellrange = CellRangeAddress.valueOf(range);
		cellrange.setLastRow(sheet.getLastRowNum());
		ref.setF(cellrange.formatAsString(sheet.getSheetName(),true));
		ref.unsetMultiLvlStrCache();//.unsetStrCache();
		//设置pt个数，也就是sheet中的行数
		CTMultiLvlStrData ctdata = ref.addNewMultiLvlStrCache();
		CTUnsignedInt pt = ctdata.addNewPtCount();
		pt.setVal(cellrange.getLastRow()-cellrange.getFirstRow() + 1);
		//循环构建lv1
		for(int icol=cellrange.getLastColumn(); icol>=cellrange.getFirstColumn(); icol--){
			CTLvl lv1 = ctdata.addNewLvl();
			for(int irow= cellrange.getFirstRow(), idx = 0; irow <= cellrange.getLastRow(); irow++,idx++){
				CTStrVal ctstrval = lv1.addNewPt();
				String val = "";
				if(sheet.getRow(irow)!=null){
					if(sheet.getRow(irow).getCell(icol)!= null){
						Cell cell =sheet.getRow(irow).getCell(icol);
						val = ExcelUtil.getCellValue2String(cell);
					}
				}
				ctstrval.setIdx(idx);
				ctstrval.setV(val);
			}
		}
	}
	
	public static void reloadCTStrRef(CTStrRef ctstrref, XSSFSheet sheet){
		if(ctstrref == null){
			return;
		}
		String range = ctstrref.getF();
		CellRangeAddress cellrange = CellRangeAddress.valueOf(range);
		cellrange.setLastRow(sheet.getLastRowNum());
		ctstrref.setF(cellrange.formatAsString(sheet.getSheetName(),true));
		ctstrref.unsetStrCache();
		CTStrData ctstrdata = ctstrref.addNewStrCache();
		for(int irow= cellrange.getFirstRow(), idx = 0; irow <= cellrange.getLastRow(); irow++,idx++){
			if(sheet.getRow(irow)!=null && sheet.getRow(irow).getCell(cellrange.getFirstColumn())!=null) {
				Cell cell =sheet.getRow(irow).getCell(cellrange.getFirstColumn());
				String val = ExcelUtil.getCellValue2String(cell);
				CTStrVal ctstrval = ctstrdata.addNewPt();
				ctstrval.setIdx(idx);
				ctstrval.setV(val);
			}
		}
	}
	
	public static void reloadCTNumRef(CTNumRef ctnumref, XSSFSheet sheet){
		if(ctnumref==null){
			return;
		}
		String range = ctnumref.getF();
		CellRangeAddress cellrange = CellRangeAddress.valueOf(range);
		cellrange.setLastRow(sheet.getLastRowNum());
		ctnumref.setF(cellrange.formatAsString(sheet.getSheetName(),true));
		ctnumref.unsetNumCache();
		CTNumData ctnumdata = ctnumref.addNewNumCache();
		for(int irow= cellrange.getFirstRow(), idx = 0; irow <= cellrange.getLastRow(); irow++,idx++){
			if(sheet.getRow(irow)!=null && sheet.getRow(irow).getCell(cellrange.getFirstColumn())!=null) {
				Cell cell =sheet.getRow(irow).getCell(cellrange.getFirstColumn());
				String strval = ExcelUtil.getCellValue2String(cell);
				CTNumVal ctnumval = ctnumdata.addNewPt();
				ctnumval.setIdx(idx);
				ctnumval.setV(strval);
			}
		}
	}
}
